package solitaire.deck;

import solitaire.core.*;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class Card extends DrawObject {

	private static String DEFAULT_PICTURE = "pic//b2fv.gif";
	private Suit suit;
	private Rank rank;
	private BufferedImage _imgImage;
	private Pile parentPile;
	private Point lastFixedPosition = new Point(0,0);
	private boolean isTurned = false;
	
	public Card(Suit suit, Rank rank){
		this.setSuit(suit);
		this.setRank(rank);
		turnCardDownSide();
	}
	
	public Card(Suit suit, Rank rank, Pile pile){
		this.setSuit(suit);
		this.setRank(rank);
		this.setPile(pile);
		turnCardDownSide();
	}
	
	public Card(int i) {
		// TODO Auto-generated constructor stub
	}

	public void setSuit(Suit suit) {
		this.suit = suit;
	}

	public Suit getSuit() {
		return suit;
	}

	public void setRank(Rank rank) {
		this.rank = rank;
	}

	public Rank getRank() {
		return rank;
	}
	
	public boolean isTurned()
	{
		return isTurned;
	}
	
	public void setPile(Pile pile) {
		this.parentPile = pile;
	}

	public Pile getPile() {
		return parentPile;
	}
	
	public Point getPoint()
	{
		Point p = new Point(0,0);
		p.setLocation(startX, startY);
		return p;
	}
	
	public void setPoint(Point point)
	{
		startX = point.getX();
		startY = point.getY();
	}
	
	public void storeLocation()
	{
		lastFixedPosition = getPoint();
	}
	
	public void restoreLocation()
	{
		setPoint(lastFixedPosition);
	}
	
	public void setLocation( double dX, double dY )
    {
        startX = dX;
        startY = dY;
    }
	
	public void drawObject( Graphics g )
    {
        Graphics2D g2 = ( Graphics2D)g;
        g2.drawRenderedImage( _imgImage, 
        		AffineTransform.getTranslateInstance( 
        				startX - _imgImage.getWidth()/2 , 
        				startY - _imgImage.getHeight()/2 ) );
    }

    public boolean contains( Point2D point )
    {
        return point.getX() >= (startX - _imgImage.getWidth()/2) && 
        	point.getX() <= (startX + _imgImage.getWidth()/2) &&
        	point.getY() >= (startY - _imgImage.getHeight()/2) && 
            point.getY() <= (startY + _imgImage.getHeight()/2);
    }
    
    public void hide()
	{
//		try
//        {
            _imgImage = null;
//        }
//        catch(IOException ex)
//        {
//            System.out.println("Could not find picture:" + DEFAULT_PICTURE);
//        }
//        
//        isTurned = false;
	}
    
    public void turnCardDownSide()
	{
		try
        {
            _imgImage = ImageIO.read( new File(DEFAULT_PICTURE) );
        }
        catch(IOException ex)
        {
            System.out.println("Could not find picture:" + DEFAULT_PICTURE);
            System.exit( -1 );
        }
        
        isTurned = false;
	}
    
    public void turnCardUpSide() {
		Integer i = rank.getNr() + 1;
		String type = "c";
		if(suit == Suit.Clubs)
			type = "c";
		if(suit == Suit.Diamonds)
			type = "d";
		if(suit == Suit.Hearts)
			type = "h";
		if(suit == Suit.Spades)
			type = "s";
		
		try
        {
            _imgImage = ImageIO.read( new File("pic//" + type + i.toString() +  ".gif") );
        }
        catch(IOException ex)
        {
            System.out.println("pic//"+ type + i.toString() +  ".gif");
            System.exit( -1 );
        }
        
        isTurned = true;
	}
    
    public CardCollection getTrailList()
	{
    	CardCollection result = new CardCollection();
		boolean trailMe = false;
        for( Card card : this.getPile().getCards())
        {
        	if(card == this)
        	{
        		trailMe = true;
        	}
        	if(trailMe)
        	{
        		result.add(card);
        	}
        }
        return result;
	}
    
    public void topMost()
    {
    	//_imgImage.
    }
    
    public String toString()
    {
    	return suit + " " + rank; 
    }
}
